<?php

$keytype = 51;
$lastNo = $this->getGameLastNo($keytype);
$opencode = $zddata ? $zddata : randKeys(10);

$redis_key = 'tianyi:bprize:jssc:';
$hasdata = common_redis()->get($redis_key . $lastNo['actionNo']);
if (!empty($hasdata)) {
    $opencode = $hasdata;
}
$cai_setting = $this->getRow("select * from {$this->prename}setting where cid={$keytype} limit 1");

if ($cai_setting['enable'] == 1) {

    $sql = "select uid from {$this->prename}bets where actionNo={$lastNo['actionNo']} and type={$keytype} GROUP BY uid";
    $sql_sum = "select sum(money) as money from {$this->prename}bets where actionNo={$lastNo['actionNo']} and type={$keytype}";
    $uids = $this->getRows($sql);
    $sum_money = $this->getRow($sql_sum);
    if (count($uids) > 0 && (count($uids) >= intval($cai_setting['count_num']) || intval($sum_money['money']) >= intval($cai_setting['count_money']))) {
        $sql = "select sum(money) as money,id,actionData,playedGroup,uid,odds from {$this->prename}bets where actionNo={$lastNo['actionNo']}  and type={$keytype} GROUP BY playedGroup,actionData  ORDER BY money desc";
        $data = $this->getRows($sql);
        $sql = "select sum(money) as money,playedGroup from {$this->prename}bets where actionNo={$lastNo['actionNo']}   and type={$keytype} GROUP BY playedGroup  ORDER BY money desc";
        $poistion_count = $this->getRows($sql);
        $res = [];
        foreach ($data as $key => $v) {
            $res[$v['playedGroup']][] = $v;
        }
        $codes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        $res2 = [];

        foreach ($poistion_count as $key => $v) {
            $dd = $res[$v['playedGroup']];
            $code = [];
            foreach ($dd as $key => $c) {
                if (count($code) <= 9) {
                    if (!in_array(intval($c['actionData']), $code)) {
                        array_push($code, intval($c['actionData']));
                    }
                } else {
                    break;
                }
            }
            $diff_code = array_diff($codes, $code);
            $res2[$v['playedGroup']] = $diff_code;
        }
        //多种组合选最优
        $total_list = [];
        for ($i = 0; $i <= 1000; $i++) {
            $total_res = $this->count_js_sylot($res, $res2, $codes);
            if (!in_array($total_res, $total_list)) {
                $total_list[] = $total_res;
            }
            if ($total_res['money'] == 0) { //有最小值不用再循环了,也许可以小于100都行
                break;
            }
        }
        $total_list = sortByMultiCols($total_list, array('money' => SORT_ASC));
        $first_data = reset($total_list);
        $rand_lists = [];
        foreach ($total_list as $key => $v) {
            if ($v['money'] == $first_data['money']) {
                $rand_lists[] = $v;
            }
        }
        shuffle($rand_lists);
        $random_keys = array_rand($rand_lists, 1);
        $total_data = $rand_lists[$random_keys];
        $opencode = $total_data['code'];
        write_log(['firstdata' => $total_data, 'lastNo' => $lastNo]);
    }
}


header('Content-type: application/xml');
echo'<?xml version="1.0" encoding="utf-8"?>';
echo '<xml><row expect="' . $lastNo['actionNo'] . '" opencode="' . $opencode . '" opentime="' . $lastNo['actionTime'] . '"/></xml>';

/* 生成随机数 */

function randKeys($len) {
    $array = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
    $charsLen = count($array) - 1;
    shuffle($array);
    $output = "";
    //  for ($i=0; $i<$len; $i++){

    $a = $array[mt_rand(0, $charsLen)];
    $b = $array[mt_rand(0, $charsLen)];
    while ($a == $b) {
        $b = $array[mt_rand(0, $charsLen)];
    }
    $c = $array[mt_rand(0, $charsLen)];
    while ($c == $a || $c == $b) {
        $c = $array[mt_rand(0, $charsLen)];
    }

    $d = $array[mt_rand(0, $charsLen)];
    while ($d == $a || $d == $b || $d == $c) {
        $d = $array[mt_rand(0, $charsLen)];
    }
    $e = $array[mt_rand(0, $charsLen)];
    while ($e == $a || $e == $b || $e == $c || $e == $d) {
        $e = $array[mt_rand(0, $charsLen)];
    }
    $f = $array[mt_rand(0, $charsLen)];
    while ($f == $a || $f == $b || $f == $c || $f == $d || $f == $e) {
        $f = $array[mt_rand(0, $charsLen)];
    }
    $g = $array[mt_rand(0, $charsLen)];
    while ($g == $a || $g == $b || $g == $c || $g == $d || $g == $e || $g == $f) {
        $g = $array[mt_rand(0, $charsLen)];
    }
    $h = $array[mt_rand(0, $charsLen)];
    while ($h == $a || $h == $b || $h == $c || $h == $d || $h == $e || $h == $f || $h == $g) {
        $h = $array[mt_rand(0, $charsLen)];
    }
    $i = $array[mt_rand(0, $charsLen)];
    while ($i == $a || $i == $b || $i == $c || $i == $d || $i == $e || $i == $f || $i == $g || $i == $h) {
        $i = $array[mt_rand(0, $charsLen)];
    }
    $j = $array[mt_rand(0, $charsLen)];
    while ($j == $a || $j == $b || $j == $c || $j == $d || $j == $e || $j == $f || $j == $g || $j == $h || $j == $i) {
        $j = $array[mt_rand(0, $charsLen)];
    }
    //$output .= $array[mt_rand(0, $charsLen)].",";
    //  }
    return $outpuet = $a . ',' . $b . ',' . $c . ',' . $d . ',' . $e . ',' . $f . ',' . $g . ',' . $h . ',' . $i . ',' . $j;
    // return rtrim($output, ',');
}

?>